/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 *
 * http://www.gnu.org/copyleft/gpl.html
 */

package l1j.server.server.clientpackets;

import java.sql.Timestamp;

import l1j.opqlo.NewSystem.各項記錄輸出;
import l1j.server.echo.ClientThread;
import l1j.server.server.model.Instance.L1ItemInstance;
import l1j.server.server.model.Instance.L1NpcInstance;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.model.Instance.L1PetInstance;
import l1j.server.server.serverpackets.S_ServerMessage;

import java.util.logging.Level;
import java.util.logging.Logger;
// Referenced classes of package l1j.server.server.clientpackets:
// ClientBasePacket

/**
 * 處理收到由客戶端傳來刪除身上道具的封包
 */
public class C_DeleteInventoryItem extends ClientBasePacket {

	private static final String C_DELETE_INVENTORY_ITEM = "[C] C_DeleteInventoryItem";
	private static final Logger _log = Logger.getLogger(C_DeleteInventoryItem.class.getName());
	@Override
	public String getType() {
		return C_DELETE_INVENTORY_ITEM;
	}

	@Override
	public void start(final byte[] decrypt, final ClientThread client) {
		try {
			// 資料載入
			this.read(decrypt);

			final L1PcInstance pc = client.getActiveChar();
			if (pc == null) {
				return;
			}
			final int itemObjectId = this.readD();
			final L1ItemInstance item = pc.getInventory().getItem(itemObjectId);
			int deleteCount = 0;

			// 沒有要刪除的道具
			if (item == null) {
				return;
			}

			if (!pc.isGm() && item.getItem().isCantDelete()) {
				// \f1你不能夠放棄此樣物品。
				pc.sendPackets(new S_ServerMessage(125));
				return;
			}
			// 使用中的寵物項鍊 - 無法刪除
			for (final L1NpcInstance petNpc : pc.getPetList().values()) {
				if (petNpc instanceof L1PetInstance) {
					final L1PetInstance pet = (L1PetInstance) petNpc;
					if (item.getId() == pet.getItemObjId()) {
						// \f1%0は捨てたりまたは他人に讓ることができません。
						pc.sendPackets(new S_ServerMessage(210, item.getItem().getName()));
						return;
					}
				}
			}
			// 使用中的魔法娃娃 - 無法刪除
			/*for (final L1DollInstance doll : pc.getDollList().values()) {
				if (doll.getItemObjId() == item.getId()) {
					pc.sendPackets(new S_ServerMessage(1181)); // 這個魔法娃娃目前正在使用中
					return;
				}
			}*/
			if(pc.getDollList().getDollByItem(item.getId()) !=null){
				pc.sendPackets(new S_ServerMessage(1181));
			}
			if (item.isEquipped()) {
				// \f1削除できないアイテムや装備しているアイテムは捨てられません。
				pc.sendPackets(new S_ServerMessage(125));
				return;
			}
			if (item.getBless() >= 128) { // 封印された装備
				// \f1%0は捨てたりまたは他人に讓ることができません。
				pc.sendPackets(new S_ServerMessage(210, item.getItem().getName()));
				return;
			}

			//opqlo 防洗 pc.getInventory().removeItem(item, item.getCount());
			if(item.getCount() > 1){
				deleteCount = this.readD();
				pc.getInventory().removeItem(item, deleteCount);
			} else {
				pc.getInventory().removeItem(item);
				//	pc.getInventory().deleteItem(item);//opqlo 防洗
			}
			pc.turnOnOffLight();
			StringBuilder aa=new StringBuilder()
			.append(pc.getNetConnection().getIp().toString())
			.append(" IP ")
			.append(pc.getName())
			.append("	刪除 【")
			.append(item.getViewName())
			.append("】	數量:");
			if(deleteCount >0){
				aa.append(deleteCount);
			}else{
				aa.append("全部");
			}
			aa.append(" 流水號:[")
			.append(item.getId())
			.append("]	時間:	")
			.append(new Timestamp(System.currentTimeMillis()))
			.append( "");
			各項記錄輸出.LogSave(13, aa.toString());
			aa.delete(0, aa.length());
			aa = null;
		} catch (final Exception e) {
			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);

		} finally {
			this.over();
		}

	}
}
